package com.wwtech.epp.ui.activity.main.presenter;

import android.support.annotation.NonNull;

import com.socks.library.KLog;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.data.api.HttpAPIWrapper;
import com.wwtech.epp.entity.Token;
import com.wwtech.epp.entity.UserLogin;
import com.wwtech.epp.ui.activity.main.MainActivity;
import com.wwtech.epp.ui.activity.main.contract.MainContract;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.main
 * @Description: presenter of MainActivity
 * @date 2017/08/02 08:43:44
 */
public class MainPresenter implements MainContract.MainContractPresenter{

    HttpAPIWrapper httpAPIWrapper;
    private final MainContract.View mView;
    private CompositeDisposable mCompositeDisposable;
    private MainActivity mActivity;

    @Inject
    public MainPresenter(@NonNull HttpAPIWrapper httpAPIWrapper, @NonNull MainContract.View view, MainActivity activity) {
        mView = view;
        this.httpAPIWrapper = httpAPIWrapper;
        mCompositeDisposable = new CompositeDisposable();
        this.mActivity = activity;
    }
    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        if (!mCompositeDisposable.isDisposed()) {
             mCompositeDisposable.dispose();
        }
    }

    @Override
    public void userLogin(Map map) {
//        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.userLogin(map)
                .subscribe(new Consumer<UserLogin>() {
                    @Override
                    public void accept(UserLogin user) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        //mView.closeProgressDialog();
                        ContainValue.userLogin = user;
                        doGetToken();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        throwable.printStackTrace();
                        //mView.closeProgressDialog();
                        //ToastUtil.show(mActivity, mActivity.getString(R.string.loading_failed_1));
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void doGetToken() {
        try {
            Map<String, String> map = new HashMap<>();
            map.put("uid", ContainValue.userLogin.getData().getUserId() + "");
            getUserToken(map);
        } catch (NullPointerException e) {
            e.printStackTrace();
            mView.getTokenFailed();
        }
    }

    @Override
    public void getUserToken(Map map) {
        //        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.getImToken(map)
                .subscribe(new Consumer<Token>() {
                    @Override
                    public void accept(Token token) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.setToekn(token.getData().getToken());
                        //mView.closeProgressDialog();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        throwable.printStackTrace();
                        //mView.closeProgressDialog();
                        //ToastUtil.show(mActivity, mActivity.getString(R.string.loading_failed_1));
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }
}